namespace CSharpPro._1106
{
    public class UserService : IUserService
    {
        private readonly List<User> _users;

        public UserService(List<User> users)
        {
            _users = users;
        }

        public void Register()
        {
            Console.Clear();
            Console.WriteLine("===== 用户注册 =====");

            string userName = ReadUserName();
            string password = ReadPassword();
            _users.Add(new User(userName, password));

            Console.WriteLine($"\n注册成功！用户名：{userName}");
            Thread.Sleep(1000);
        }

        public void Login()
        {
            Console.Clear();
            Console.WriteLine("===== 用户登录 =====");

            Console.Write("请输入用户名：");
            string name = Console.ReadLine()?.Trim() ?? string.Empty;
            Console.Write("请输入密码：");
            string pwd  = Console.ReadLine()?.Trim() ?? string.Empty;
            User cur = null;
            foreach (var u in _users)
            {
                if (u.UserName == name && u.Password == pwd)
                {
                    cur = u;
                    break;
                }
            }

            if (cur == null)
            {
                Console.WriteLine("登录失败：用户名或密码错误！");
            }
            else
            {
                Console.WriteLine($"欢迎 {cur.UserName} 登录系统！");
            }
            Thread.Sleep(1000);
        }

        public void ShowMainMenu()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 查看所有用户");
                Console.WriteLine("4. 删除用户");
                Console.WriteLine("0. 退出");
                Console.Write("请选择：");

                switch (Console.ReadLine()?.Trim())
                {
                    case "1": Login(); break;
                    case "2": Register(); break;
                    case "3":
                        Console.Clear();
                        Console.WriteLine("当前注册用户：");
                        foreach (var u in _users)
                            Console.WriteLine(u.UserName);
                        Console.WriteLine("按任意键返回...");
                        Console.ReadKey();
                        break;
                    case "4":
                        Console.Clear();
                        if (_users.Count == 0)
                        {
                            Console.WriteLine("暂无用户可删除！");
                            Thread.Sleep(500);
                            break;
                        }
                        for (int i = 0; i < _users.Count; i++) 
                        {
                            Console.WriteLine($"{i + 1}. {_users[i].UserName}");
                        }
                        Console.Write("请输入序号删除（0 返回）：");
                        if (int.TryParse(Console.ReadLine(), out int idx) && idx > 0 && idx <= _users.Count)
                        {
                            _users.RemoveAt(idx - 1);
                            Console.WriteLine("删除完成！");
                        }
                        Thread.Sleep(500);
                        break;
                    case "0":
                        Console.WriteLine("Bye~");
                        return;
                    default:
                        Console.WriteLine("无效选择！");
                        Thread.Sleep(500);
                        break;
                }
            }
        }

        #region 私有辅助方法
        private string ReadUserName()
        {
            while (true)
            {
                Console.Write("用户名（3-15位，字母数字下划线）：");
                string s = Console.ReadLine()?.Trim() ?? string.Empty;
                if (string.IsNullOrEmpty(s))
                {
                    Console.WriteLine("不可为空！");
                    continue;
                }
                if (s.Length < 3 || s.Length > 15 || !System.Text.RegularExpressions.Regex.IsMatch(s, @"^[a-zA-Z0-9_]+$"))
                {
                    Console.WriteLine("格式不符！");
                    continue;
                }
                if (_users.Any(u => u.UserName == s))
                {
                    Console.WriteLine("已存在！");
                    continue;
                }
                return s;
            }
        }

        private string ReadPassword()
        {
            while (true)
            {
                Console.Write("密码（6-20位，含大写、小写、数字）：");
                string s = Console.ReadLine()?.Trim() ?? string.Empty;
                if (string.IsNullOrEmpty(s))
                {
                    Console.WriteLine("不可为空！");
                    continue;
                }
                bool upper = s.Any(char.IsUpper);
                bool lower = s.Any(char.IsLower);
                bool digit = s.Any(char.IsDigit);
                if (s.Length < 6 || s.Length > 20 || !upper || !lower || !digit)
                {
                    Console.WriteLine("强度不够！");
                    continue;
                }
                Console.Write("再次确认密码：");
                string again = Console.ReadLine()?.Trim() ?? string.Empty;
                if (again != s)
                {
                    Console.WriteLine("两次不一致！");
                    continue;
                }
                return s;
            }
        }
        #endregion
    }
}